﻿using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xcj.Mes.WholeLine.DataBase.Entity;
using Xcj.Mes.WholeLine.Plc.Factory;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.ViewModels
{
    public class MainViewModel : BaseViewModel
    {
        private string _version = "2025.09.25 10:15";
        public string Version
        {
            get => _version;
            set
            {
                _version = value;
                RaisePropertyChanged();
            }
        }

        private string _schedule = "白班";
        public string Schedule
        {
            get => _schedule;
            set
            {
                _schedule = value;
                RaisePropertyChanged();
            }
        }

        private DateTime _currentTime = DateTime.Now;
        public DateTime CurrentTime
        {
            get => _currentTime;
            set
            {
                _currentTime = value;
                RaisePropertyChanged();
            }
        }

        public DeviceConfigEntity deviceConfigEntity
        {
            get => App.deviceConfigEntity;
            set
            {
                App.deviceConfigEntity = value;
                RaisePropertyChanged();
            }
        }

        private System.Timers.Timer timer = new System.Timers.Timer();

        private Dictionary<string, string> _dicPlcStatus = new Dictionary<string, string>();
        public Dictionary<string, string> DicPlcStatus => _dicPlcStatus;

        public MainViewModel(IRegionManager regionManager)
        {
            // 初始化状态字典
            var processNames = new[] { "钉卷", "组立", "老化", "座板" };
            foreach (var name in processNames)
            {
                _dicPlcStatus[name] = "DarkGray";
            }
            PlcFactory.Init(_eventAggregator);

            timer.Interval  = 1000;
            timer.AutoReset = true;
            timer.Elapsed  += new System.Timers.ElapsedEventHandler(async (sender, e) =>
            {
                try { timer?.Stop(); } catch { }
                await Task.Run(() =>
                {
                    try
                    {
                        CurrentTime = DateTime.Now;
                        if ((CurrentTime.Hour >= 8) && (CurrentTime.Hour < 20))
                            Schedule = "白班";
                        else
                            Schedule = "晚班";
                    }
                    catch { }
                });
                try { timer?.Start(); }catch { }
            });
            timer.Start();
            
            try
            {
                string currentFile = System.Diagnostics.Process.GetCurrentProcess()?.MainModule?.FileName;
                if (!string.IsNullOrEmpty(currentFile))
                {
                    DateTime fileTime = System.IO.File.GetLastWriteTime(currentFile);
                    Version = fileTime.ToString("yyyy-MM-dd HH:mm"); // 系统版本
                }
            }
            catch { }


            // 订阅PLC状态变化事件
            PlcFactory.PlcStatusChanged += (s, plcName) => UpdatePlcStatus(plcName);
        }

        private void UpdatePlcStatus(string plcName)
        {
            if (_dicPlcStatus.ContainsKey(plcName))
            {
                string newStatus;

                if (!PlcFactory.plcFactory.ContainsKey(plcName))
                {
                    newStatus = "DarkGray";
                }
                else
                {
                    newStatus = PlcFactory.plcFactory[plcName].IsConnected ? "Lime" : "Red";
                }

                if (_dicPlcStatus[plcName] != newStatus)
                {
                    _dicPlcStatus[plcName] = newStatus;
                    RaisePropertyChanged(nameof(DicPlcStatus));
                }
            }
        }
    }
}
